
package com.jf.cloud.user.controller.platform;

import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.user.dto.UserOrgDTO;
import com.jf.cloud.user.model.UserOrg;
import com.jf.cloud.user.service.UserOrgService;
import com.jf.cloud.user.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * @author zz
 * @date 2023-03-01 20:20:59
 */
@RestController("platformUserOrgController")
@RequestMapping("/p/user_org")
@Tag(name = "platform-会员单位表")
public class UserOrgController {

    @Autowired
    private UserOrgService userOrgService;

    @Autowired
    private UserService userService;

    @Autowired
    private MapperFacade mapperFacade;

    @GetMapping("/page")
    @Operation(summary = "获取单位列表", description = "单位列表数据")
    public ServerResponseEntity<PageVO<UserOrg>> page(@Valid PageDTO pageDTO) {
        PageVO<UserOrg> userOrgPage = userOrgService.page(pageDTO);
        return ServerResponseEntity.success(userOrgPage);
    }

    @GetMapping
    @Operation(summary = "获取单位", description = "单位数据")
    public ServerResponseEntity<UserOrg> getById(@RequestParam Long id) {
        return ServerResponseEntity.success(userOrgService.getById(id));
    }

    @PostMapping
    @Operation(summary = "保存会员单位", description = "保存会员单位数据")
    public ServerResponseEntity<Void> save(@Valid @RequestBody UserOrgDTO userOrgDTO) {
        UserOrg userOrg = mapperFacade.map(userOrgDTO, UserOrg.class);
        userOrg.setId(null);
        userOrgService.save(userOrg);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新会员单位", description = "更新会员单位数据")
    public ServerResponseEntity<Void> update(@Valid @RequestBody UserOrgDTO userOrgDTO) {
        UserOrg userOrg = mapperFacade.map(userOrgDTO, UserOrg.class);
        userOrgService.update(userOrg);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除会员单位", description = "删除会员单位数据")
    public ServerResponseEntity<Void> delete(@RequestParam Long id) {
        userOrgService.deleteById(id);
        return ServerResponseEntity.success();
    }

    @GetMapping("count_org_user")
    @Operation(summary = "获取单位和单位用户数量", description = "获取单位和单位用户数量")
    public ServerResponseEntity<Map<String,Object>> countOrgUser() {
        return ServerResponseEntity.success(userOrgService.countOrg());
    }
}
